package com.liehu.business.service;

import com.liehu.business.dao.ResidenceMapper;
import com.liehu.business.pojo.Employee;
import com.liehu.business.pojo.Residence;
import io.jsonwebtoken.Claims;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Residence的service层
 */
@Service
@Transactional
public class ResidenceService {
    @Autowired
    private ResidenceMapper residenceMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private JwtUtil jwtUti;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;//redis

    /**
     * 查询所有
     */
    public List<Residence> findAll(){
        return residenceMapper.selectList(null);
    }

    /**
     * 根据Id查询
     */
    public Residence findById(String id){
        return residenceMapper.selectById(id);
    }


    /**
     * 插入
     */
    public void save(Residence residence){
        Claims claims_customer = (Claims) request.getAttribute("claims_customer");
        if(claims_customer == null ){
            throw new RuntimeException("权限不足！");
        }
        //设置订单主键
        residence.setId(idWorker.nextId()+"");
        //设置订单的状态
        residence.setStatus("1");
        //获取当前登录客户的的id
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(claims_admin == null){
            throw  new RuntimeException("权限不足！");
        }
        //获取id，并将Id存入
        String customerId = claims_admin.getId();
        residence.setCustomerId(customerId);


        //1.先从缓存中获取业务员队列
        List<Employee> employeeList = (List<Employee>) redisTemplate.opsForValue().get("employeeList");
        if(employeeList == null || employeeList.size()<1){
            throw  new RuntimeException("业务员信息异常！");
        }
        //2.取出队列的首位业务员，然后存入到对象里用于保存
        Employee employee = employeeList.get(0);
        //封装信息
        residence.setEmployeeId(employee.getId());
        residence.setEmployeeName(employee.getRealName());
        Map<String,String> map = new HashMap<>();
        map.put("state","1");
        //将业务员队列消费掉的信息告知Rabbit
        rabbitTemplate.convertAndSend("employeeList",map);

        //插入数据
        residenceMapper.insert(residence);
    }

    /**
     * 根据id更新
     */
    public void update(Residence residence){
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        Claims claims_emplyee = (Claims) request.getAttribute("claims_emplyee");
        if(claims_admin != null ||  claims_emplyee != null){
            residenceMapper.updateById(residence);
        }else{
            throw new RuntimeException("权限不足！");
        }


    }


    /**
     * 根据id删除
     */
    public void delete(String id){
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(claims_admin == null ){
            throw new RuntimeException("权限不足！");
        }
        residenceMapper.deleteById(id);
    }

}
